/**
 * 获取当前需要展示的课程
 * @param courseList {array} 章列表
 * @returns {array} 当前需要展示的课程
 */
export function getCurrentCurriculum (courseList) {
  return courseList
    // 根据courseList每一项的curriculum_list平铺数组
    .reduce((pre, course) => [...pre, ...course.curriculum_list], [])
    .find(c => c.is_study === 1) || this.courseList[0].curriculum_list[0]
}

/**
 * 将is_study === 1 的父节点isOpen设置为true
 * 如果没有is_study === 1则将第一个的isOpen设置为true
 * @param courseList {array} 章列表
 * @returns {array} 添加了isOpen属性的章列表
 */
export function courseSetIsOpen (courseList) {
  let setCourse = courseList.reduce((pre, course) => [...pre,
    Object.assign(course,
      {isOpen: course.curriculum_list.some(curriculum => curriculum.is_study === 1)}
    )], [])
  if (setCourse.every(c => c.isOpen === false)) {
    setCourse[0].isOpen = true
  }
  return setCourse
}
